package cn.sylinx.horm.example.section1;

import java.io.Serializable;

import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Component;

import cn.sylinx.horm.core.SqlClient;
import cn.sylinx.horm.dialect.fs.FS;
import cn.sylinx.horm.example.entity.MyExample;

@Profile("section1")
@Component
public class Section1 implements CommandLineRunner {

    private SqlClient sqlClient;

    public Section1(SqlClient sqlClient) {
        this.sqlClient = sqlClient;
    }

    @Override
    public void run(String... args) throws Exception {

        // 创建对象
        MyExample my = new MyExample();
        my.setName("my_name");
        my.setNumber("my_number");
        Serializable pk = sqlClient.save(my);
        System.out.println("save pk -> " + pk);

        // 更新对象
        MyExample myUpdate = new MyExample();
        myUpdate.setId(Long.parseLong(pk.toString()));
        myUpdate.setName("my_name_updated");
        int count = sqlClient.update(myUpdate);
        System.out.println("update count -> " + count);

        // 查询
        MyExample myQuery = sqlClient.get(pk, MyExample.class);
        System.out.println("get object -> " + myQuery);

        // 查询
        MyExample myQuery1 = sqlClient.queryFirst(FS.of(MyExample.class).where(MyExample::getId).eq(pk));
        System.out.println("fs query object -> " + myQuery1);

        // 删除
        MyExample myDelete = new MyExample();
        myDelete.setId(Long.parseLong(pk.toString()));
        sqlClient.delete(myDelete);
    }
}
